package com.routing.simple;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.util.Random;

public class ReceiveLogsDirectSimple {
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EmitLogDirectSimple.MYDIRECT, BuiltinExchangeType.DIRECT);

        String queueName = channel.queueDeclare().getQueue();
        Random random = new Random();
        int i = random.nextInt(3);
        if(i==1){
            channel.queueBind(queueName, EmitLogDirectSimple.MYDIRECT, "error");
        }else {
            channel.queueBind(queueName, EmitLogDirectSimple.MYDIRECT, "info");
            channel.queueBind(queueName, EmitLogDirectSimple.MYDIRECT, "warning");
            channel.queueBind(queueName, EmitLogDirectSimple.MYDIRECT, "error");
        }

        channel.basicConsume(queueName,true,(tag,message)->{
            String msg = new String(message.getBody(), StandardCharsets.UTF_8);

            System.out.println("发送过来的消息的路由key：" + message.getEnvelope().getRoutingKey() + "  消息内容是：" + msg);

        },(consumeTag)->{});

    }
}
